package com.mogujie.tt.ui.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.mogujie.tt.R;
import com.mogujie.tt.ui.base.TTBaseFragment;


/**
 * Created by Administrator on 2015/10/20.
 */
public class MpbiFragment extends TTBaseFragment {

    private View curView = null;
    private TextView DeviceName,DeviceCpuModel,Devicedriver,brand,amainboard,Fingerprint;
    private TextView tvp,tvb,tvo,GpsJw,GpsDz;

    private LocationClient mLocationClient;
    public MyLocationListener mMyLocationListener;
    private LocationMode tempMode = LocationMode.Hight_Accuracy;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle bundle) {
        if (null != curView) {
            ((ViewGroup) curView.getParent()).removeView(curView);
            return curView;
        }
        curView = inflater.inflate(R.layout.tt_fragment_mpbi, topContentView);
        initRes();
        return curView;
    }

    private void initRes() {

        setTopTitle(getActivity().getString(R.string.phone_page_name));
        setTopLeftButton(R.drawable.tt_top_back);
        topLeftContainerLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getActivity().finish();
            }
        });
        setTopLeftText(getResources().getString(R.string.top_left_back));

        DeviceName = (TextView) curView.findViewById(R.id.DeviceName);
        DeviceCpuModel = (TextView) curView.findViewById(R.id.DeviceCpuModel);
        Devicedriver = (TextView) curView.findViewById(R.id.Devicedriver);
        brand = (TextView) curView.findViewById(R.id.brand);
        amainboard = (TextView) curView.findViewById(R.id.amainboard);
        Fingerprint = (TextView) curView.findViewById(R.id.Fingerprint);

        tvb = (TextView) curView.findViewById(R.id.tvb);
        tvo = (TextView) curView.findViewById(R.id.tvo);
        tvp = (TextView) curView.findViewById(R.id.tvp);
        GpsJw = (TextView) curView.findViewById(R.id.GpsJw);
        GpsDz = (TextView) curView.findViewById(R.id.GpsWz);

        tvp.setText(Build.MODEL);
        tvb.setText(Build.VERSION.SDK);
        tvo.setText(Build.VERSION.RELEASE);
        DeviceName.setText(android.os.Build.PRODUCT);
        DeviceCpuModel.setText(android.os.Build.CPU_ABI);
        Devicedriver.setText(android.os.Build.DEVICE);
        brand.setText(android.os.Build.BRAND);
        amainboard.setText(android.os.Build.BOARD);
        if(Build.FINGERPRINT.toString().length()>10) {
            Fingerprint.setText(android.os.Build.FINGERPRINT.substring(0, 20) + "...");
        }else{
            Fingerprint.setText(android.os.Build.FINGERPRINT);
        }
        GpsIn();

    }
    private void GpsIn() {
        mLocationClient = new LocationClient(getActivity().getApplicationContext());
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(false);
        option.setLocationMode(tempMode);
        option.setCoorType("gcj02");
        option.setProdName("SXUNLancher");
        option.setScanSpan(1000);
        option.setIsNeedAddress(true);
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    @Override
    protected void initHandler() {

    }

    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub

            if (location == null) {
                return;
            }
            String str="";
            try {
                double latitude=location.getLatitude();
                double longitude=location.getLongitude();
                GpsJw.setText("经度："+longitude+"  纬度："+latitude);
                if(location.getAddrStr()!=null && !"".equals(location.getAddrStr())){
                    str+=location.getAddrStr();
                }
                if(latitude==4.9E-324){
                    GpsJw.setText("定位失败");
                }
                if(longitude==4.9E-324){
                    GpsJw.setText("定位失败");
                }
                System.out.println("asd="+str);
                GpsDz.setText(str+"");
            } catch (Exception e) {}
        }
    }
}
